package org.lanyu.springainovel.common.config;

import org.lanyu.springainovel.common.repository.CustomJdbcChatMemoryRepository;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 自定义Spring AI Chat Memory配置�? * 使用自定义的JdbcChatMemoryRepository实现，支持content_index排序字段
 */
@Configuration
public class CustomSpringAIChatMemoryConfig {

    @Value("${spring.ai.chat.memory.max-messages}")
    private int maxMessages;

    /**
     * 创建自定义JDBC聊天记忆仓库Bean
     * 
     * @param jdbcTemplate JDBC模板
     * @return CustomJdbcChatMemoryRepository实例
     */
    @Bean
    public CustomJdbcChatMemoryRepository customJdbcChatMemoryRepository(JdbcTemplate jdbcTemplate) {
        return new CustomJdbcChatMemoryRepository(jdbcTemplate);
    }

    /**
     * 创建ChatMemory Bean
     * 使用自定义的JdbcChatMemoryRepository实现，不依赖ChatMemoryRepository接口
     * 
     * @param customJdbcChatMemoryRepository 自定义JDBC聊天记忆仓库
     * @return ChatMemory实例
     */
    @Bean
    @Primary
    public ChatMemory chatMemory(CustomJdbcChatMemoryRepository customJdbcChatMemoryRepository) {
        // 使用默认会话ID创建ChatMemory实例
        return customJdbcChatMemoryRepository.getChatMemory("default", maxMessages);
    }
}
